/**
 * IO模块
 * 推送命令viewer.publish("io.emit", params);
 * 接收命令viewer.register(cmd, ...params); 需先注册cmd命令
 */

(function () {
    var SOCKET = Arena.SOCKET;

    var _socket = null;
    //var connected = false;

    var IO = {};
    IO.connect = function () {
        try {
            _socket = io.connect(SOCKET.CONFIG.IP, SOCKET.CONFIG.OPTION);

            _socket.on("router", function (data) {
                utils.log(Base64.decode(data));
                var _data = $.extend(true, {}, JSON.parse(Base64.decode(data)));

                utils.log("接收数据<<<=====,时间：" + Date.now());
                utils.log(_data);
                utils.log("\n");

                IO.update(_data);
            });

            _socket.on("connect", function(){
                
                if(SOCKET.CONNECTED){
                	return;
                }
                SOCKET.CONNECTED = true;

                viewer.publish("io.connect");

            });

            _socket.on('connect_error', function(error) {

                viewer.publish("io.error", [{
                    type : "Connect_Error",
                    error : error
                }]);

            });

            _socket.on('reconnect', function() {

                viewer.publish("io.reconnect");

            });

            _socket.on('reconnecting', function() {

                viewer.publish("io.reconnecting");

            });

            _socket.on('disconnect', function() {

                viewer.publish("io.disconnect");

            });
        } catch (error) {

            viewer.publish("io.error", [{
                type : "Uncaught_Error",
                error : error
            }]);

        }
        return this;
    }

    IO.emit = function (data) {
        utils.log("推送命令======>>>>,时间：" + Date.now());
        utils.log(data);
        utils.log("\n");

        _socket.emit("router", Base64.encode(JSON.stringify(data)));
    }

    IO.update = function (data) {
        if(data.code == 200){
            viewer.publish(data.cmd, data.rep, data.cmd, data.code);
        }else{
            viewer.publish("cmd.error", data.cmd, data.code, data.rep || data.msg);
        }
    }

    IO.Event = {
        onConnect : function () {
            utils.log("成功连接到服务器:", SOCKET.CONFIG.IP);
        },

        onError : function (error) {
            utils.log("连接服务器错误，请稍后刷新重试。错误信息：", error);
        },

        onReconnect : function () {
            utils.log('成功重新连接到服务器');
        },

        onReconnecting : function () {
            utils.log('重新连接服务器...');
        },

        onDisconnect : function () {
            utils.log('与服务器断开连接');
        }
    }

    IO.Interface = {
        disconnect : function () {
            _socket.disconnect();
        }
    }

    //IO模块监听游戏开始事件
    viewer.register("arena.start", IO.connect);

    //注册IO模块接口
    viewer.register("io.emit", IO.emit);

    viewer.register("io.connect", IO.Event.onConnect);
    viewer.register("io.error", IO.Event.onError);
    viewer.register("io.reconnect", IO.Event.onReconnect);
    viewer.register("io.reconnecting", IO.Event.onReconnecting);
    viewer.register("io.disconnect", IO.Event.onDisconnect);
    viewer.register("io.event.disconnect", IO.Interface.disconnect);
})();